package com.sopt.friends;

import java.util.ArrayList;

import com.sopt.friends.statics.FriendsStaticVars;
import com.sopt.friends.sub.FreshActivity;
import com.sopt.friends.util.FriendsContactsManager;

import com.sopt.friends.vo.FriendsCommonItem;
import com.sopt.view.HomeMenuAdapter;
import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class FirstActivity extends Activity {

	private ListView ListView01;
	private ListView ListView02;
	FriendsContactsManager man;
	ArrayList<FriendsCommonItem> infoMenuList;
	ArrayList<FriendsCommonItem> infoMenuByDateList;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first);

		ListView01 = (ListView) findViewById(R.id.listFirst);
		ListView02 = (ListView) findViewById(R.id.listSecond);
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		if(man==null)
		man = new FriendsContactsManager(this,
				getContentResolver());
		
		infoMenuList = man.getInfoMenuList();
		infoMenuByDateList = man.getInfoByDateMenuList();
		
		HomeMenuAdapter infomenuAdapter = new HomeMenuAdapter(this,
				R.layout.row_homemenu_layout, infoMenuList);

		ListView01.setAdapter(infomenuAdapter);

		ListView01.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				FriendsCommonItem item = (FriendsCommonItem) ListView01.getItemAtPosition(position);
				if(item.getNewcount()>0)
					getNextAction(item.getNextaction());
				else
					view.findViewById(R.id.rowHomeMenuNextActionButton).setVisibility(ImageView.INVISIBLE);
			}
		});

		HomeMenuAdapter infomenuByDateAdapter = new HomeMenuAdapter(this,
				R.layout.row_homemenu_layout, infoMenuByDateList);

		ListView02.setAdapter(infomenuByDateAdapter);
		
		ListView02.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				FriendsCommonItem item = (FriendsCommonItem) ListView02.getItemAtPosition(position);
				if(item.getNewcount()>0)
					getNextAction(item.getNextaction());
			}
		});
	}
	
	private void getNextAction(int action){
		switch(action){
		case FriendsStaticVars.ACTION_FIRST_TO_FRESH :
			startActivity(new Intent(this,FreshActivity.class).setAction(String.valueOf(action)));
			break;
		case FriendsStaticVars.ACTION_FIRST_TO_BIRTH :
			startActivity(new Intent(this,FreshActivity.class).setAction(String.valueOf(action)));
			break;
		case FriendsStaticVars.ACTION_FIRST_TO_NEWS :
			startActivity(new Intent(this,FreshActivity.class).setAction(String.valueOf(action)));
			break;
		case FriendsStaticVars.ACTION_FIRST_TO_BEST :
			startActivity(new Intent(this,FreshActivity.class).setAction(String.valueOf(action)));
			break;
		case FriendsStaticVars.ACTION_FIRST_TO_RECENTLY :
			startActivity(new Intent(this,FreshActivity.class).setAction(String.valueOf(action)));
			break;
		case FriendsStaticVars.ACTION_FIRST_TO_FORGOTTON :
			startActivity(new Intent(this,FreshActivity.class).setAction(String.valueOf(action)));
			break;
		default :
			break;
			
		}
	}

}